import 'package:flutter/material.dart';
import 'package:flutter_first_project/pages/home/index.dart';
import 'package:flutter_first_project/pages/mine/index.dart';
import 'package:flutter_first_project/utils/Emitter.dart';
import 'package:flutter_first_project/utils/TokenManager.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super.key});

  @override
  _TabsPageState createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
   List tabsList = [
    {
      'label': '首页',
      'icon': 'assets/tabs/home_default.png',
      'activeIcon': 'assets/tabs/home_active.png'
    },
    {
      'label': '我的',
      'icon': 'assets/tabs/my_default.png',
      'activeIcon': 'assets/tabs/my_active.png'
    }
  ];
   int _currentIndex = 1; // 当前选中的索引值
// 获取底部导航栏的列表项
 getItem(){
  List<BottomNavigationBarItem> list = [];
  for(var item in tabsList){
    list.add(BottomNavigationBarItem(
      icon: Image.asset(item['icon']!,width: 24,height: 24,),
      activeIcon: Image.asset(item['activeIcon']!,width: 24,height: 24,),
      label: item['label']!,
    ));
  }
   return list;
}

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    tokenManager.init(); // 初始化token
    regiterEventBus(); // 注册监听事件
  }
  regiterEventBus(){
     eventBus.on<LogoutEvent>().listen((params) {
      // 退出登录
      Navigator.pushNamed(context, "/login");
    });
  }

  // 退出登录
  void updateIndex(int index){
    setState(() {
      _currentIndex = index;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:SafeArea(
        child: IndexedStack(
          index: _currentIndex, // 当前选中的索引值
          children:  [
          const  HomePage(),
          MinePage(currentIndex: _currentIndex, updateIndex:updateIndex)]
        )
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed, // 底部导航栏的类型
        items: getItem(),
        unselectedItemColor: Colors.black87,
        selectedItemColor: const Color.fromRGBO(99, 144, 174, 1), // 选中的颜色
        currentIndex: _currentIndex, // 当前选中的索引值
        onTap: (int index){
          setState(() {
            _currentIndex = index; // 当前选中的索引值
           
          });
        },
      )
    );
  }
}
